
let input = document.getElementById("input")
let btn = document.getElementsByClassName("button")[0];
let text = document.getElementById("textInput");
let bai = document.getElementsByClassName("bai")[0];
let nei = document.getElementsByClassName("nei")[0];
let showUrl = document.getElementById("showUrl");
btn.onclick = function (){
    input.click();
    
}
input.onchange = function (){
    let file = this.files[0];
    uploadFile(file,(url)=>{
        console.log(url);
        showUrl.value = "https://" + url;
    })
}

function uploadFile(file, callBack) {
    // console.log(file)
    let strName = text.value?text.value : "niming";
    let name = file.name.split(".")[0] + "." + file.type.split("/")[1];
    let url = "otherUserObject/" + strName + "/" + name;
    const cos = new COS({
        // 必选参数
        SecretId: 'AKIDEG2XliPqzrTosfBRQ8ZjcEZdQv96mAH7',
        SecretKey: 'atvgBOKyLSIr9AtMYT8WcRzGFyQbU6SX',
    })
    cos.putObject({
        Bucket: 'more-time-1302371948', /* 必须 */
        Region: 'ap-shanghai',
        Key: url,
        StorageClass: 'STANDARD',
        Body: file, // 上传文件对象
        onProgress: function (progressData) {
            // console.log(progressData)
            bai.innerText = progressData.percent*100;
            nei.style.width = parseInt(progressData.percent*100) + "%";
        }
    }, function (err, data) {
        // console.log(err || data.Location)
        callBack(data.Location)
    })
}